<?php
namespace App\Http\Controllers\Business\Admin\Development;

use Auth;
use Exception;
use App\Models\User;
use App\Models\ShopSalesmanDetail;
use App\Models\SalesmanRegister;
use App\Utils\DeviceTool;


class FindUsersManagement{
    //查找用户信息
    public static function findUserInfoByUserMobile($mobile){
        $data = [];
        if(!$mobile){
            return ['status'=>'success','data'=>$data];
        }
        $userMobiles = explode(',',$mobile);
        if(empty($userMobiles) || !is_array($userMobiles)){
            return ['status'=>'failed','message'=>'请将号码已逗号分割搜索'];
        }
        //去掉重复的电话号码
        $userMobiles = array_unique($userMobiles);
        foreach($userMobiles as $key=>$value){
            if(strstr($value, '*')) {
                //如果手机号码加密了，带*
                $mobileArr = explode('****', $value);
                $mobileFirstPart = isset($mobileArr[0]) ? $mobileArr[0] : '';
                $mobileLastPart = isset($mobileArr[1]) ? $mobileArr[1] : '';
                if (!$mobileFirstPart || !$mobileLastPart) {
                    return ['status' => 'failed', 'message' => '手机号码输错了'];
                }
                $mobileLists = User::where('mobile', 'like', $mobileFirstPart . '%')->where('mobile', 'like', '%' . $mobileLastPart)->get();
            } else {
                //如果手机号码没有加密
                $mobileLists = User::where('mobile', $value)->get();
            }
            if(isset($mobileLists) && !empty($mobileLists)){
                foreach($mobileLists as $k=>$v){
                    //获取相同设备号的用户mobile
                    $sameDeviceMobiles = self::byDeviceFindMobile($v['id'],$v['deviceid']);
                    //获取用户是否是地推商家并判断是否有效
                    $salemanShopInfo = self::salesmanShopEffective($v['shopid'],$v['mobile']);
                    //获取用户是否是地推用户并判断是否有效
                    $salemanUserInfo = self::salesmanUserEffective($v['mobile']);
                    //通过IP获取地区
                    if(!empty($v['ip'])){
                        $ipResult = DeviceTool::getCityByIp($v['ip']);
                        if($ipResult['status'] == 'failed') {
                            $ipAddress = '';
                        } else {
                            $ipAddress = $ipResult['data']['province'] . $ipResult['data']['city'];
                        }
                    }else{
                        $ipAddress = '';
                    }
                    //通过手机号获取归属地
                    $homeLocationResult = DeviceTool::getMobileHomeLocation($v['mobile']);
                    if($homeLocationResult['status'] == 'failed') {
                        $homeLocation = "";
                    } else {
                        $homeLocation = $homeLocationResult['data'];
                    }
                    array_push($data, [
                        'mobile'=>$v['mobile'],
                        'userip'=>$v['ip'],
                        'registerarea'=>$v['registerarea'],
                        'samedevicemobiles'=>$sameDeviceMobiles,
                        'salemanshopinfo'=>$salemanShopInfo,
                        'salemanuserinfo'=>$salemanUserInfo,
                        'ipaddress' => $ipAddress,
                        'homelocation' => $homeLocation,
                    ]);
                }
            }
        }
        return ['status'=>'success','data'=>$data];
    }
    //输入手机号码后(如138xxxx7986)位获取用户的基本信息
    public static function findUserInfoByMobile($mobile) {
        if(!$mobile) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if(strstr($mobile, '*')) {
            //如果手机号码加密了，带*
            $mobileArr = explode('****', $mobile);
            $mobileFirstPart = isset($mobileArr[0]) ? $mobileArr[0] : '';
            $mobileLastPart = isset($mobileArr[1]) ? $mobileArr[1] : '';
            if (!$mobileFirstPart || !$mobileLastPart) {
                return ['status' => 'failed', 'message' => '手机号码输错了'];
            }
            $mobileLists = User::where('mobile', 'like', $mobileFirstPart . '%')->where('mobile', 'like', '%' . $mobileLastPart)->get();
        } else {
            //如果手机号码没有加密
            $mobileLists = User::where('mobile', $mobile)->get();
        }
        $lists = [];
        foreach($mobileLists as $mobileList) {
            $userId = $mobileList['id'];
            $deviceId = $mobileList['deviceid'];
            $sameDeviceMobiles = self::byDeviceFindMobile($userId,$deviceId);
            $shopId = $mobileList['shopid'];
            $mobile = $mobileList['mobile'];
            $ip = $mobileList['ip'];
            $shopSalesmanInfo = self::salesmanShopEffective($shopId, $mobile);
            $userSalesmanInfo = self::salesmanUserEffective($mobile);
            $ipResult = DeviceTool::getCityByIp($ip);
            if($ipResult['status'] == 'failed') {
                $ipAddress = '';
            } else {
                $ipAddress = $ipResult['data']['province'] . $ipResult['data']['city'];
            }
            $homeLocationResult = DeviceTool::getMobileHomeLocation($mobile);
            if($homeLocationResult['status'] == 'failed') {
                $homeLocation = "";
            } else {
                $homeLocation = $homeLocationResult['data'];
            }
            $list = [
                'mobile' => $mobile,
                'nickname' => $mobileList['nickname'],
                'created_at' => $mobileList['created_at'],
                'ip' => $ip,
                'ipaddress' => $ipAddress,
                'homelocation' => $homeLocation,
                'registerarea' => $mobileList['registerarea'],
                'samedevicemobiles' => $sameDeviceMobiles,
                'salemanshopinfo' => $shopSalesmanInfo,
                'salemanuserinfo'=>$userSalesmanInfo,
            ];
            array_push($lists, $list);
        }
        return ['status'=>'success', 'data'=>$lists];
    }
    //通过相同的设备号获取电话号码
    protected static function byDeviceFindMobile($userId,$userDeviceId){
        $userDeviceIds = '';
        if($userDeviceId){
            $userInfo = User::where('id','<>',$userId)->where('deviceid',$userDeviceId)->select('mobile')->get();
            if(isset($userInfo) && !empty($userInfo)){
                foreach($userInfo as $key=>$value){
                    if(!empty($value['mobile'])){
                        $userDeviceIds .= $value['mobile'].";";
                    }
                }
                $userDeviceIds = trim($userDeviceIds,';');
            }
        }
        return $userDeviceIds;
    }
    //获取用户是否有地推商家并且是否有效
    protected static function salesmanShopEffective($shopId,$userMobile){
        $salesmanShopEffective = '不是';
        if($shopId){
            $salesmanShopInfo = ShopSalesmanDetail::where('shopid',$shopId)->where('mobile',$userMobile)
                ->where('type', 'register')->select('getmoney')->first();
            if($salesmanShopInfo){
                if(!empty($salesmanShopInfo['getmoney'])){
                    $salesmanShopEffective = '是/有效';
                }else{
                    $salesmanShopEffective = '是/无效';
                }
            }
        }
        return $salesmanShopEffective;
    }
    //获取用户是否有地推用户并且判断是否有效
    protected static function salesmanUserEffective($userMobile){
        $salesmanUserEffective = '不是';
        $salesmanUserInfo = SalesmanRegister::where('registermobile',$userMobile)->select('isvalid')->first();
        if($salesmanUserInfo){
            if(!empty($salesmanUserInfo['isvalid'])){
                $salesmanUserEffective = '是/有效';
            }else{
                $salesmanUserEffective = '是/无效';
            }
        }
        return $salesmanUserEffective;
    }
}